package com.gimi.cloud.bbp.dao.dao.prod;

import com.gimi.cloud.bbp.dao.dao.sys.BaseDao;
import com.gimi.cloud.bbp.dao.myMapper.MyMapper;
import com.gimi.cloud.bbp.dto.prod.EcProdCatModelValueDTO;
import com.gimi.cloud.bbp.entity.prod.EcProdCatModelValueEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
*  商品模版属性值dao
* @author loongs
* @Description
* @create 2017-12-18
* @version 1.0
**/
@Mapper
public interface EcProdCatModelValueDao extends MyMapper<EcProdCatModelValueEntity>,BaseDao<EcProdCatModelValueEntity,EcProdCatModelValueDTO> {

    /**
     * 根据modelId和modelValue查询不在指定模版属性值id中的总数
     * @param modelId 模版属性id
     * @param modelValue 模版属性值
     * @param cmId 排除的模版属性值id
     * @return 获取结果
     * @throws Exception
     */
    @Select("SELECT count(*) FROM ec_prod_cat_model_value val WHERE val.is_delete = 0 and val.model_id = #{modelId} and val.model_value =#{modelValue} AND cm_id != ifNULL(#{cmId},-1)")
    int selectCountByModelIdAndModelValueExcludeCmId(@Param("modelId")Integer modelId,@Param("modelValue")String modelValue,@Param("cmId")Integer cmId) throws Exception;

    /**
     * 根据模版id获取模版值集合
     * @param modelId 模版id
     * @return 查询结果
     * @throws Exception
     */
    @Select("SELECT * FROM ec_prod_cat_model_value WHERE is_delete = 0 AND  model_id = #{modelId}")
    List<EcProdCatModelValueDTO> queryPordCatModelValueByModelId(@Param("modelId")Integer modelId) throws  Exception;


    @Select("SELECT cm_id,model_id,model_value,is_img FROM ec_prod_cat_model_value WHERE cm_id = #{cmId};")
    EcProdCatModelValueDTO getModelIdById(@Param(value = "cmId") Integer cmId);
    
    @Select("select a.`prod_prop_name` AS skuDetailKey,b.model_value AS skuDetailValue from ec_prod_cat_model_value b inner join ec_prod_cat_model a on a.`model_id` = b.`model_id` where a.is_delete=0 and b.is_delete=0 and  b.cm_id in(${cmIds})")
    List<Map<String,Object>> getModelIdByIds(@Param(value = "cmIds") String cmIds);

    @Select("SELECT cm_id FROM ec_prod_cat_model_value WHERE model_id = #{modelId};")
    Set<String> selectCmIdByModelId(@Param(value = "modelId") Integer modelId);

    @Select("SELECT model_value FROM ec_prod_cat_model_value WHERE model_id = #{modelId};")
	List<String> selectModelValueByModelId(@Param(value = "modelId") Integer modelId);
}
